package es.jclm.agri.foragri.model;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import es.jclm.agri.foragri.services.EmpleadoService;


public class EmpleadoLazyDataModel extends LazyDataModel<Empleado>{
	private static final long serialVersionUID = -8832831134966938627L;

	private SearchCriteria searchCriteria;

	private EmpleadoService empleadoService;

	private List<Empleado> empleados;

	private Empleado selected;


	public void setEmpleadoService(EmpleadoService empleadoService) {
		this.empleadoService = empleadoService;
	}

	public void setSearchCriteria(SearchCriteria searchCriteria) {
		this.searchCriteria = searchCriteria;
	}

	public List<Empleado> load() {
		//this.searchCriteria.setCurrentPage(first / pageSize + 1);
		//this.empleados = empleadoService.findEmpleados(searchCriteria, first, sortField, order.equals(SortOrder.ASCENDING));
		this.empleados = empleadoService.listAll();
		return empleados;
	}
	
	@Override
	public List<Empleado> load(int first, int pageSize, String sortField, SortOrder order, Map<String, String> filters) {
		this.searchCriteria.setCurrentPage(first / pageSize + 1);
		//this.empleados = empleadoService.findEmpleados(searchCriteria, first, sortField, order.equals(SortOrder.ASCENDING));
		this.empleados = empleadoService.listAll();
		return empleados;
	}

	@Override
	public Empleado getRowData(String rowKey) {
		for (Empleado empleado : this.empleados){
			if (empleado.getId().equals(rowKey)) {
				return empleado;
			}
 		}
		return null;
	}

	@Override
	public Object getRowKey(Empleado empleado) {
		return empleado.getId();
	}

	@Override
	public int getRowCount() {
		int ret = empleadoService.getNumberOfEmpleados(searchCriteria); 
		System.out.println("filas = "+ret);
		return ret;
	}

	public Empleado getSelected() {
		return selected;
	}

	public void setSelected(Empleado selected) {
		this.selected = selected;
	}

	public int getCurrentPage() {
		return this.searchCriteria.getCurrentPage();
	}

	public int getPageSize() {
		return this.searchCriteria.getPageSize();
	}

	public List<Empleado> getEmpleados() {
		return empleados;
	}

	public void setEmpleados(List<Empleado> empleados) {
		this.empleados = empleados;
	}
	
	

}
